#include "addfrienddialog.h"
#include "debug.h"

/********************************
 * 表示一个好友搜索的结果
 *******************************/

FriendResultItem::FriendResultItem(const UserInfo &userInfo)
    :userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    // 2. 创建布局管理器
    QGridLayout* glayout = new QGridLayout();
    glayout->setSpacing(0);
    glayout->setContentsMargins(5,0,5,0);
    this->setLayout(glayout);

    // 3. 创建头像
    QPushButton* avatar = new QPushButton();
    avatar->setFixedSize(50,50);
    avatar->setIconSize(QSize(50,50));
    avatar->setIcon(userInfo.avatar);

    // 4. 创建昵称
    QLabel* nameLabel = new QLabel();
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700;}");
    nameLabel->setText(userInfo.nickname);

    // 5. 创建个性签名
    QLabel* descLabel  = new QLabel();
    descLabel ->setFixedHeight(35);
    descLabel ->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descLabel ->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel ->setStyleSheet("QLabel { font-size: 14px;}");
    descLabel ->setText(userInfo.description);

    // 6. 创建添加好友按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(100,40);
    addBtn->setText("添加好友");
    QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); color: rgb(255, 255, 255); border-radius: 10px;} ";
    btnStyle +=  "QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    addBtn->setStyleSheet(btnStyle);

    // 7. 把上述内容, 添加到布局管理器中
    glayout->addWidget(avatar,0,0,2,1);
    glayout->addWidget(nameLabel,0,1);
    glayout->addWidget(descLabel,1,1);
    glayout->addWidget(addBtn,0,2,2,1);
}


/********************************
 * 整个搜索好友的窗口
 *******************************/

AddFriendDialog::AddFriendDialog(QWidget *parent)
    :QDialog(parent)
{
    // 1. 设置基本属性
    this->setFixedSize(400,500);
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setAttribute(Qt::WA_TranslucentBackground);

    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 自定义标题栏
    QWidget *titleBar = new QWidget(this);
    titleBar->setStyleSheet("background-color: rgb(243,243,243);border-radius: 15px;");
    titleBar->setFixedSize(400,30);

    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);
    hlayout->setAlignment(Qt::AlignRight);
    titleBar->setLayout(hlayout);

    QLabel *titleLabel = new QLabel();
    // titleLabel->setFixedSize(340,30);
    titleLabel->setFixedHeight(30);
    titleLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    titleLabel->setText("添加好友");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setStyleSheet("color: black; font-weight: bold;");

    // 添加关闭按钮
    QPushButton *closeButton = new QPushButton();
    closeButton->setFixedSize(30,30);
    closeButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    closeButton->setText("X");
    closeButton->setStyleSheet("QPushButton { color: balck; border: none;} QPushButton:pressed { background-color: rgb(255, 0, 0); } QPushButton:hover { background-color: rgb(255, 0, 0); }");
    connect(closeButton, &QPushButton::clicked, this, &QDialog::close);

    // 布局
    hlayout->addWidget(titleLabel);
    hlayout->addWidget(closeButton);
    vlayout->addWidget(titleBar);

    // 2. 添加布局管理器
    QWidget* searchWidget = new QWidget();
    searchWidget->setFixedSize(400,470);
    vlayout->addWidget(searchWidget);

    glayout = new QGridLayout();
    glayout->setSpacing(5);
    glayout->setContentsMargins(10,0,10,0);
    glayout->setAlignment(Qt::AlignTop);
    searchWidget->setLayout(glayout);

    // 3. 创建搜索框
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(40);
    searchEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    searchEdit->setPlaceholderText("按手机号/用户序号/昵称搜索");
    QString searchStyle = "QLineEdit { border: none; border-radius: 10px; font-size: 16px; background-color: rgb(235, 235, 235); padding-left: 5px;}";
    searchEdit->setStyleSheet(searchStyle);
    glayout->addWidget(searchEdit,0,0,1,8);

    // 4. 创建搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(40,40);
    searchBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    searchBtn->setIconSize(QSize(40,40));
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString BtnStyle = "QPushButton { border: none; background-color: rgb(240, 240, 240); border-radius: 10px; }";
    BtnStyle += "QPushButton:hover { background-color: rgb(220, 220, 220); } QPushButton:pressed { background-color: rgb(200, 200, 200); } ";
    searchBtn->setStyleSheet(BtnStyle);
    glayout->addWidget(searchBtn,0,8,1,1);

    // 5. 添加滚动区域
    initResultArea();

    // 构造假的数据, 验证界面效果
#if TEST_UI
    QIcon avatar(":/resource/image/defaultAvatar.png");
    for(int i = 0;i < 30;i++)
    {
        // new 出来这个对象, 再往 addResult 中添加. FriendResultItem 中持有了 UserInfo 的 const 引用. 需要确保引用是有效的引用
        UserInfo* user = new UserInfo();
        user->userId = QString::number(100+i);
        user->nickname = "小短腿" + QString::number(i);
        user->description = "哈哈哈哈哈哈哈哈哈哈";
        user->avatar = avatar;
        this->addFriendResultItem(*user);
    }
#endif
}

void AddFriendDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        event->accept();
    }
}

void AddFriendDialog::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        this->move(event->globalPosition().toPoint() - m_dragPosition);
        event->accept();
    }
}

void AddFriendDialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 3. 绘制圆角矩形背景
    QPainterPath path;
    path.addRoundedRect(rect(), 15, 15);  // 圆角半径
    painter.fillPath(path, QColor("#F3F3F3"));
}

void AddFriendDialog::initResultArea()
{
    // 1. 创建滚动区域对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    // 设置滚动条相关的样式
    QString style = "QScrollBar:vertical {border: none;background: rgb(243,243,243);width: 10px;margin: 0px 0px 0px 0px;}\
        QScrollBar::handle:vertical {background: #c1c1c1;min-height: 20px;border-radius: 5px;}\
        QScrollBar::handle:vertical:hover {background: #a8a8a8;}\
        QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {height: 0px;}\
        QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {background: none;}";
    scrollArea->verticalScrollBar()->setStyleSheet(style);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0;} ");
    scrollArea->setStyleSheet("QScrollArea { border: none; }");
    glayout->addWidget(scrollArea,1,0,1,9);

    // 2. 创建 QWidget
    resultWidget = new QWidget();
    resultWidget->setObjectName("resultWidget");
    resultWidget->setStyleSheet("#resultWidget { background-color: rgb(243, 243, 243); }");
    scrollArea->setWidget(resultWidget);

    // 3. 给这个 QWidget 里面添加元素, 需要给它创建垂直的布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0,0,0,0);
    resultWidget->setLayout(vlayout);
}

void AddFriendDialog::addFriendResultItem(const UserInfo &userinfo)
{
    FriendResultItem* friendResItem = new FriendResultItem(userinfo);
    resultWidget->layout()->addWidget(friendResItem);
}

void AddFriendDialog::clear()
{
    QVBoxLayout* vlayout = dynamic_cast<QVBoxLayout*>(resultWidget->layout());
    for(int i = vlayout->count()-1;i >= 0;--i)
    {
        auto* item = vlayout->takeAt(i);    // takeAt就会进行移除
        if(item == nullptr || item->widget() == nullptr)
        {
            continue;
        }
        delete item->widget();      // 释放空间
    }
}

